/*
 * @Descripttion: GGB
 * @version: 1.0
 * @Author: GGB
 * @Date: 2021-08-22 12:08:24
 * @LastEditors: GGB
 * @LastEditTime: 2021-08-22 13:05:38
 */
import 'package:flutter/material.dart';
import 'package:flutter_data_center/components/custom_btn.dart';
import 'package:flutter_data_center/components/custom_textfield_widget.dart';
import 'package:flutter_data_center/conf/config.dart';
import 'package:flutter_data_center/utills/color_utils.dart';
import 'package:flutter_data_center/utills/sp_utils.dart';
import 'package:flutter_data_center/utills/toast_utils.dart';

class ConfigScreen extends StatefulWidget {
  const ConfigScreen({Key key}) : super(key: key);

  @override
  _ConfigScreenState createState() => _ConfigScreenState();
}

class _ConfigScreenState extends State<ConfigScreen> {
  FocusNode _urlFocusNode = new FocusNode();

  TextEditingController _urlEditController =
      new TextEditingController(text: Config.BASE_HOST);
  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) => Scaffold(
        appBar: buildAppBar(),
        body: SafeArea(
          child: SingleChildScrollView(
            physics: BouncingScrollPhysics(),
            child: Padding(
              padding: const EdgeInsets.symmetric(horizontal: 20, vertical: 10),
              child: Column(
                mainAxisAlignment: MainAxisAlignment.start,
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  SizedBox(height: 30),
                  Text("服务器地址：", style: TextStyle(fontSize: 18)),
                  SizedBox(height: 10),
                  buildUrl(context),
                  SizedBox(height: 30),
                  CustomBtn(
                    text: '完成',
                    onTap: () async {
                      print("点击");
                      await SPUtils.save("baseUrl", _urlEditController.text);
                      await Config.init();
                      print(Config.BASE_HOST);
                      Navigator.of(context).pop();
                    },
                    height: 45,
                    width: 365,
                    style: TextStyle(fontSize: 14),
                    primary: gColor("#08D187"),
                  ),
                ],
              ),
            ),
          ),
        ),
      );

  TextFieldWidget buildUrl(BuildContext context) {
    return TextFieldWidget(
      submit: (value) {
        if (value == null) {
          ToastUtils.showToast("请输入服务器地址");
          FocusScope.of(context).requestFocus(_urlFocusNode);
          return;
        }
        _urlFocusNode.unfocus();
        FocusScope.of(context).requestFocus(_urlFocusNode);
      },
      maxLines: 3,
      focusNode: _urlFocusNode,
      controller: _urlEditController,
      obscureText: false,
    );
  }

  //  构建appBar
  AppBar buildAppBar() {
    return AppBar(
      backgroundColor: Colors.transparent,
      centerTitle: true,
      title: Text(
        "配置",
        style: TextStyle(
          fontSize: 18,
        ),
      ),
      elevation: 0,
    );
  }
}
